package com.zx.cloud.config;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import feign.RequestInterceptor;
import feign.RequestTemplate;

/**
 * @author Alex
 */
@Configuration
public class BeanConfiguration {

	@Bean
	public HystrixRequestFilter hystrixRequestFilter() {
		return new HystrixRequestFilter();
	}

	@Bean
	public RequestInterceptor requestInterceptor() {
		return new RequestInterceptor() {
			@Override
			public void apply(RequestTemplate requestTemplate) {
				HttpServletRequest request = HttpContextHystrixRequestVariable.getInstance().get();
				Enumeration<String> headerNames = request.getHeaderNames();
				if (headerNames != null) {
					while (headerNames.hasMoreElements()) {
						String name = headerNames.nextElement();
						String values = request.getHeader(name);
						requestTemplate.header(name, values);
					}
				}
				requestTemplate.header("x-base-auth", "token");
			}
		};
	}

}
